package com.example.demo2_review.design_pattern.creational_patterns.factory_pattern;

import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author zwp14
 * @create 2024/6/24 14:38
 * @description 工厂模式-测试接口
 */
@RestController
@RequestMapping("/factory_pattern")
@RequiredArgsConstructor
public class TestDemoController {
    private final Rectangle rectangle;
    private final Circle circle;



    public static void main(String[] args) {
        // 使用工厂类获取对象
        ShapeFactory shapeFactory = new ShapeFactory();
        // 获取 Circle 的对象，并调用它的 draw 方法
        ShapeService circle = shapeFactory.getShape("Circle");
        circle.draw();
        // 获取 Rectangle 的对象，并调用它的 draw 方法
        ShapeService rectangle = shapeFactory.getShape("Rectangle");
        rectangle.draw();
    }





    @GetMapping("/testDemo1")
    public String testDemo1(@RequestParam String condition) {

        /*
        * 这里的if和else if相当于另一种工厂模式应用场景：当一个类希望由他的子类来指定所创建的对象的时候*/
        if (StringUtils.equals("rectangle", condition)) {
            return rectangle.draw();
        } else if ("circle".equalsIgnoreCase(condition)) {
            return circle.draw();
        } else if (condition.contains("rectangle") && condition.contains("circle")) {
            return "查询不合法！";
        }
        return "未查询到相关数据！";
    }


}
